﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.DSJCYD94;
using JM.JCY.Proxy;
using System.Linq;

namespace JM.JCY.SharePointComponent.CaseManagement.CaseSearch
{
    public partial class CaseSearchUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    this.ddlDepartment.DataSource = proxy.GetAllDepartment();
                    ddlDepartment.DataTextField = "DeptName";
                    ddlDepartment.DataValueField = "DeptName";
                    ddlDepartment.DataBind();
                    ListItem lt = new ListItem();
                    lt.Text = "请选择一个部门..";
                    lt.Value = string.Empty;
                    ddlDepartment.Items.Insert(0, lt);
                }
            }
        }

        protected void imgSearch_Click(object sender, ImageClickEventArgs e)
        {
            GridViewDataBind();
        }

      
       

        protected void gvCaseSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCaseSearch.PageIndex = e.NewPageIndex;
            GridViewDataBind();
        }

        public T_AJGL_AJCXDataSet.T_AllDataDataTable GetAJGL_AJCX(string CBR, string CBBM, string startDate, string endDate, string N_AJLB)
        {
            using (DSJCYD94Proxy proxy = new DSJCYD94Proxy())
            {
                T_AJGL_AJCXDataSet.T_AllDataDataTable dt = proxy.GetAJGL_AJCX(CBR, CBBM, startDate, endDate, N_AJLB);
                return dt;
            }
        }

        private void GridViewDataBind()
        {
            string startDate = "1900-01-01";
            string endDate = "8900-01-01";
            if (!dtDateFrom.IsDateEmpty)
            {
                startDate = dtDateFrom.SelectedDate.ToString("yyyy-MM-dd");
            }

            if (!dtDateTo.IsDateEmpty)
            {
                endDate = dtDateTo.SelectedDate.ToString("yyyy-MM-dd");
            }
            string CBR = txtCBR.Text.Trim();
            string CBBM = ddlDepartment.SelectedIndex == 0 ? string.Empty : ddlDepartment.SelectedValue;
            string N_AJLB = this.ddlCaseCategory.SelectedValue == "0" ? string.Empty : ddlCaseCategory.SelectedValue;
            var DT = GetAJGL_AJCX(CBR, CBBM, startDate, endDate, N_AJLB);
            if (RadioButtonList1.SelectedIndex == 0)
            {
                gvCaseSearch.DataSource = DT;
            }
            else if (RadioButtonList1.SelectedIndex == 1)
            {
                DT.DefaultView.RowFilter = "D_JARQ is not null";
                gvCaseSearch.DataSource = DT.DefaultView;
            }
            else if (RadioButtonList1.SelectedIndex == 2)
            {
                DT.DefaultView.RowFilter = "D_JARQ is null";
                gvCaseSearch.DataSource = DT.DefaultView;
            }

            if (DT.DefaultView.Count > 0)
                this.NoRecord.Visible = false;
            else
                this.NoRecord.Visible = true;

            this.gvCaseSearch.DataBind();
        }
    }
}
